/************************************************/
/************************************************/
/*** PART 4.4: TEACHER - STUDENT ANALYSIS  ***/
/************************************************/
/************************************************/


*********************************************
*** INDIVIDUAL NETWORK MEASURES AS OUTCOMES
*********************************************

	global covar1 male ageinm braven_sd beyes_sd baseline_tmale tmarried baseline_tchildnum ttenured baseline_tage baseline_texperience bteyes_sd btraven_sd b_gms b_pedagogy b_modern b_extrinsic b_warmth b_competence b_idealism csize refshare astudent s_ref_exposure

	global covar1a male ageinm braven_sd beyes_sd baseline_tmale tmarried baseline_tchildnum ttenured baseline_tage baseline_texperience bteyes_sd btraven_sd b_gms b_pedagogy b_modern b_extrinsic b_warmth b_competence b_idealism csize refshare astudent s_ref_exposure

	global covar1b male ageinm braven_sd beyes_sd  baseline_tmale tmarried baseline_tchildnum ttenured baseline_tage baseline_texperience bteyes_sd btraven_sd b_gms b_pedagogy b_modern b_extrinsic b_warmth b_competence b_idealism csize refshare astudent s_ref_exposure

** DEGREE CENTRALITY
** IT ASSIGNS AN IMPORTANCE SCORE BASED PURELY ON THE NUMBER OF LINKS HELD BY EACH NODE

	** FRIENDSHIP-DEGREE

	reg bnode_in_friend biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec5a
	reg bnode_in_friend biat_ethnic $covar1a i.grade1 i.b_schoolid  if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec5b

		** TESTING ACROSS 

		reg bnode_in_friend biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1
		estimates store m1
		reg bnode_in_friend biat_ethnic $covar1a i.grade1 i.b_schoolid  if refugee==1 & sample1==1
		estimates store m2
		suest m1 m2, cluster(b_classid)
		test [m1_mean]biat_ethnic=[m2_mean]biat_ethnic
		estadd scalar p1=r(p),  : spec5b

	** SUPPORT SELF

	reg support_received biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec1a
	reg support_received biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec1b

		** TESTING ACROSS 

		reg support_received biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1
		estimates store m1
		reg support_received biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1
		estimates store m2
		suest m1 m2, cluster(b_classid)
		test [m1_mean]biat_ethnic=[m2_mean]biat_ethnic
		estadd scalar p1=r(p),  : spec1b


	** STUDY SELF

	reg study_received biat_ethnic $covar1a i.grade1 i.b_schoolid  if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec2a
	reg study_received biat_ethnic $covar1a i.grade1 i.b_schoolid  if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec2b

		** TESTING ACROSS 

		reg study_received biat_ethnic $covar1a i.grade1 i.b_schoolid  if refugee==0 & sample1==1
		estimates store m1
		reg study_received biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1
		estimates store m2
		suest m1 m2, cluster(b_classid)
		test [m1_mean]biat_ethnic=[m2_mean]biat_ethnic
		estadd scalar p1=r(p),  : spec2b

	** SUPPORT

	reg support_provided biat_ethnic $covar1a i.grade1 i.b_schoolid  if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec3a
	reg support_provided biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec3b

		** TESTING ACROSS 

		reg support_provided biat_ethnic $covar1a i.grade1 i.b_schoolid  if refugee==0 & sample1==1
		estimates store m1
		reg support_provided biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1
		estimates store m2
		suest m1 m2, cluster(b_classid)
		test [m1_mean]biat_ethnic=[m2_mean]biat_ethnic
		estadd scalar p1=r(p),  : spec3b

	** STUDY

	reg study_provided biat_ethnic $covar1a i.grade1 i.b_schoolid  if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec4a
	reg study_provided biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec4b

		** TESTING ACROSS 

		reg study_provided biat_ethnic $covar1a i.grade1 i.b_schoolid  if refugee==0 & sample1==1
		estimates store m1
		reg study_provided biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1
		estimates store m2
		suest m1 m2, cluster(b_classid)
		test [m1_mean]biat_ethnic=[m2_mean]biat_ethnic
		estadd scalar p1=r(p),  : spec4b


	#delimit ;
	esttab spec5a spec5b spec1a spec1b spec2a spec2b spec3a spec3b spec4a spec4b using "$path2/table5.tex",  replace label compress b(%8.2f) se(2) 
	nonumbers keep(biat_ethnic)
	coeflabels(biat_ethnic "Teacher Ethnic Bias")
	star(* 0.10 ** 0.05 *** 0.01) nonotes 
	s(N p1, fmt(%9.0g %9.3f) labels("N" "P-Value (Host=Refugee)") layout(@ "\multicolumn{2}{S}{@}"))
	nogaps
	mtitles ( "Host" "Refugee" "Host" "Refugee" "Host" "Refugee" "Host" "Refugee" "Host" "Refugee")
	mgroups("Friendship""E.S. Received" "A.S. Received" "E.S. Provided" "A.S. Provided" , pattern(1 0 1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
	substitute("&\multicolumn{2}{S}{" "\multicolumn{2}{c}{")
	;
	#delimit cr

	est clear

***********************
*** REFUGEE EXCLUSION
***********************

	** NUMBER OF OUT-DEGREE HOST TO REFUGEE (FRIEND)
	
	reg bfriend_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1 , cluster(b_classid)
	estimates store spec1

	** NUMBER OF OUT-DEGREE HOST TO HOST
	
	reg bfriend_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec2

	** NUMBER OF OUT-DEGREE REFUGEE TO REFUGEE
	
	reg bfriend_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1 , cluster(b_classid)
	estimates store spec3

	** NUMBER OF OUT-DEGREE REFUGEE TO HOST
	
	reg bfriend_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec4

	** NUMBER OF OUT-DEGREE HOST TO REFUGEE (SUPPORT)
	
	reg bsupport_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1 , cluster(b_classid)
	estimates store spec5

	** NUMBER OF OUT-DEGREE HOST TO HOST
	
	reg bsupport_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec6

	** NUMBER OF OUT-DEGREE REFUGEE TO REFUGEE
	
	reg bsupport_self_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1 , cluster(b_classid)
	estimates store spec7

	** NUMBER OF OUT-DEGREE REFUGEE TO HOST  
	
	reg bsupport_self_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec8

	** NUMBER OF OUT-DEGREE HOST TO REFUGEE (STUDY)
	
	reg bstudy_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1 , cluster(b_classid)
	estimates store spec9

	** NUMBER OF OUT-DEGREE HOST TO HOST
	
	reg bstudy_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec10

	** NUMBER OF OUT-DEGREE REFUGEE TO REFUGEE
	
	reg bstudy_self_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec11

	** NUMBER OF OUT-DEGREE REFUGEE TO HOST 
	
	reg bstudy_self_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec12

/************************************************/
*** ETHNIC BIAS AND CLASS LEVEL ETHNIC SEGREGATION
/************************************************/

	preserve
	
	collapse bcoleman_ref* ageinm maleshare biat_ethnic baseline_tmale baseline_tage baseline_texperience tmarried baseline_tchildnum ttenured bteyes_sd btraven_sd b_pedagogy b_gms b_idealism b_competence b_modern b_extrinsic b_warmth csize gr1 gr2 grade1 b_schoolid b_classid baseline_tterm braven_sd bmath_std bturk_std urfa refshare b_districtid btsurvey_16_refprop (mean) male sample1 sample0 , by(btname)
	*398

** COLEMAN HOMOPHILY

	global covar3 baseline_tmale baseline_tage baseline_texperience tmarried baseline_tchildnum ttenured bteyes_sd btraven_sd b_gms b_pedagogy b_competence b_modern b_extrinsic b_warmth b_idealism refshare csize urfa male ageinm 

	
	** COMBINE RECEIVED AND PROVIDED
	
	egen bcoleman_ref1_supportall=rowmean(bcoleman_ref1_supportself bcoleman_ref1_support)
	egen bcoleman_ref2_supportall=rowmean(bcoleman_ref2_supportself bcoleman_ref2_support)
	egen bcoleman_ref1_studyall=rowmean(bcoleman_ref1_studyself bcoleman_ref1_study)
	egen bcoleman_ref2_studyall=rowmean(bcoleman_ref2_studyself bcoleman_ref2_study)


	** FRIENDSHIP
	
	char b_schoolid[omit] 12
	xi: reg bcoleman_ref1_friend biat_ethnic $covar3 gr1 gr2 i.b_schoolid if sample1==1, cluster(b_classid)
	estimates store spec13
	xi: reg bcoleman_ref2_friend biat_ethnic $covar3 gr1 gr2 i.b_schoolid if sample1==1, cluster(b_classid)
	estimates store spec14


	** SUPPORT 
	
	xi: reg bcoleman_ref1_supportall biat_ethnic $covar3 gr1 gr2 i.b_schoolid if sample1==1, cluster(b_classid)
	estimates store spec15
	xi: reg bcoleman_ref2_supportall biat_ethnic $covar3 gr1 gr2 i.b_schoolid if sample1==1, cluster(b_classid)
	estimates store spec16


	** STUDY
	
	xi: reg bcoleman_ref1_studyall biat_ethnic $covar3 gr1 gr2 i.b_schoolid if sample1==1, cluster(b_classid)
	estimates store spec17
	xi: reg bcoleman_ref2_studyall biat_ethnic $covar3 gr1 gr2 i.b_schoolid if sample1==1, cluster(b_classid)
	estimates store spec18



	
	#delimit ;
		esttab spec1 spec2  spec3 spec4 spec13 spec14 using "$path2/table6.tex", replace fragment label compress b(%8.2f) se(2) 
		nonumbers keep (biat_ethnic)
		coeflabels(biat_ethnic "\textbf{Panel 1:} Friendship Ties \\ Teacher Ethnic Bias")
		star(* 0.10 ** 0.05 *** 0.01) nonotes nogaps
		mtitles ( "Host $\rightarrow$ Refugee" "Host $\rightarrow$ Host" "Refugee $\rightarrow$ Refugee" "Refugee $\rightarrow$ Host" "Host" "Refugee")
		mgroups("Host Nominations" "Refugee Nominations" "Homophily" , pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
		substitute(_ \_ \hline\hline \hline)
	;
	#delimit cr

	#delimit ;
		esttab spec5 spec6  spec7 spec8  spec15 spec16 using "$path2/table6.tex", fragment append label compress b(%8.2f) se(2) 
		nonumbers keep (biat_ethnic)
		coeflabels(biat_ethnic "\textbf{Panel 2:} Emotional Support Ties \\ Teacher Ethnic Bias")
		star(* 0.10 ** 0.05 *** 0.01) nonotes nogaps
		nomtitles
		substitute(_ \_ \hline\hline \hline)
	;
	#delimit cr

	#delimit ;
		esttab spec9 spec10  spec11 spec12 spec17 spec18 using "$path2/table6.tex", fragment append label compress b(%8.2f) se(2) 
		nonumbers keep (biat_ethnic)
		coeflabels(biat_ethnic "\textbf{Panel 3:} Academic Support Ties \\ Teacher Ethnic Bias")
		star(* 0.10 ** 0.05 *** 0.01) nonotes /*noobs*/ nogaps
		nomtitles
		substitute(_ \_ \hline\hline \hline)
	;
	#delimit cr

	est clear
	restore


***************************************
*** ETHNIC BIAS AND REFUGEE ACHIEVEMENT
***************************************

	** VERBAL 
	
	reg bturk_std biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec3a
	reg bturk_std biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec3b

		** TEST ACROSS

		reg bturk_std biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1
		estimates store m1
		reg bturk_std biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1
		estimates store m2
		suest m1 m2, cluster(b_classid)
		test [m1_mean]biat_ethnic=[m2_mean]biat_ethnic
		estadd scalar p1=r(p),  : spec3b


	** MATH
	
	reg bmath_std biat_ethnic $covar1b i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec4a
	reg bmath_std biat_ethnic $covar1b i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec4b

		** TEST ACROSS

		reg bmath_std biat_ethnic $covar1b i.grade1 i.b_schoolid if refugee==0 & sample1==1
		estimates store m1
		reg bmath_std biat_ethnic $covar1b i.grade1 i.b_schoolid if refugee==1 & sample1==1
		estimates store m2
		suest m1 m2, cluster(b_classid)
		test [m1_mean]biat_ethnic=[m2_mean]biat_ethnic
		estadd scalar p1=r(p),  : spec4b


	** BULLYING
	
	probit d_bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid)
	margins, dydx(biat_ethnic) post
	estimates store spec1a
	probit d_bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	margins, dydx(biat_ethnic) post
	estimates store spec1b

	reg bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid)
	estimates store spec2a
	reg bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid)
	estimates store spec2b


		** TEST ACROSS

		probit d_bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==0 & sample1==1
		estimates store m1
		probit d_bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==1 & sample1==1
		estimates store m2
		suest m1 m2, cluster(b_classid)
		test [m1_d_bsbully]biat_ethnic=[m2_d_bsbully]biat_ethnic
		estadd scalar p1=r(p),  : spec1b

		reg bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==0 & sample1==1
		estimates store m1
		reg bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==1 & sample1==1
		estimates store m2
		suest m1 m2, cluster(b_classid)
		test [m1_mean]biat_ethnic=[m2_mean]biat_ethnic
		estadd scalar p1=r(p),  : spec2b

	#delimit ;
	esttab spec1a spec1b spec2a spec2b spec3a spec3b spec4a spec4b  using "$path2/table7.tex",  replace label compress b(%8.2f) se(2) 
	nonumbers keep(biat_ethnic)
	coeflabels(biat_ethnic "Teacher Ethnic Bias")
	star(* 0.10 ** 0.05 *** 0.01) s(N p1, fmt(%9.0g %9.3f) labels("N" "P-Value (Host=Refugee)") layout(@ "\multicolumn{2}{S}{@}"))
	nogaps
	nonotes
	mtitles ( "Host" "Refugee" "Host" "Refugee" "Host" "Refugee" "Host" "Refugee")
	mgroups("Probability Bullied" "Number of Bullies" "Turkish" "Mathematics", pattern(1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
	substitute("&\multicolumn{2}{S}{" "\multicolumn{2}{c}{")
	;
	#delimit cr
	est clear

